package com.qcby.controller;

import com.qcby.Constants;
import com.qcby.dao.MovieActorRelationMapper;
import com.qcby.model.Movie;
import com.qcby.model.MovieActorRelation;
import com.qcby.model.dto.MovieActorCreateDto;
import com.qcby.model.dto.MovieConsoleVo;
import com.qcby.model.dto.MovieSaveDto;
import com.qcby.model.dto.MovieQueryConsoleDto;
import com.qcby.model.resp.RespEntity;
import com.qcby.service.MovieActorRelationService;
import com.qcby.service.MovieService;
import com.qcby.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/movie")
public class MovieController {
    @Autowired
    private MovieService movieService;

    @Autowired
    private MovieActorRelationService movieActorRelationService;

    @Autowired
    private MovieActorRelationMapper movieActorRelationMapper;

    @RequestMapping("/selectAll")
    public RespEntity<List<Movie>> selectAll() {
        List<Movie> movies = movieService.selectObjectAll();
        return RespEntity.successCustomize(Constants.FLAG_200, "查询成功！", movies);
    }

    @RequestMapping("/list")
    public RespEntity<Page<MovieConsoleVo>> searchMovie(@RequestBody MovieQueryConsoleDto queryConsoleDto) {
        if (queryConsoleDto != null) {
            if (queryConsoleDto.getPageNo() == null) {
                queryConsoleDto.setPageNo(1);
            }
            Page<MovieConsoleVo> page = movieService.query(queryConsoleDto.getMtId(), queryConsoleDto.getMName(),
                    queryConsoleDto.getMStatus(), queryConsoleDto.getPageNo(), queryConsoleDto.getPageSize());

            System.out.println(page);
            return RespEntity.successCustomize(Constants.FLAG_200, "查询成功", page);
        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "参数不能为空", null);
    }

    //查看电影详情  访问MovieActorRelationController

    //添加操作
    @RequestMapping("/addMovie")
    public RespEntity<String> addMovie(@RequestBody MovieSaveDto newMovie) {
        if (newMovie != null) {
            Movie movie = new Movie(newMovie);
            Integer midNew = movieService.insert(movie);

            List<MovieActorCreateDto> actorList = newMovie.getActorList();
            for (MovieActorCreateDto createDto : actorList) {
                movieActorRelationService.insert(new MovieActorRelation(midNew, createDto));
            }
            return RespEntity.successCustomize(Constants.FLAG_200, "", "添加成功");
        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "", "添加失败");
    }

    //删除（下架操作） 只将电影的状态改为下架
    @RequestMapping("/takeOffMovie")
    public RespEntity<String> deleteMovie(Integer mid) {
        if (mid != null) {
            movieService.takeOffMovie(mid);
            return RespEntity.successCustomize(Constants.FLAG_200, "", "下架成功");
        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "", "下架失败");
    }

    //删除（下架操作） 只将电影的状态改为下架
    @RequestMapping("/delete/{mid}")
    public RespEntity<String> delete(@PathVariable Integer mid) {
        if (mid != null) {
            movieService.deleteByPrimaryKey(mid);
            return RespEntity.successNoData("下架成功");
        }
        return RespEntity.errorNoData(Constants.FLAG_400, "下架失败");
    }

    //根据ID查询
    @RequestMapping("/getMovieById")
    public RespEntity<Movie> getMovieById(Integer mid) {
        if (mid != null) {
            Movie movie = movieService.selectByPrimaryKey(mid);
            System.out.println(movie);
            return RespEntity.successCustomize(Constants.FLAG_200, "", movie);
        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "", null);
    }

    @RequestMapping("/updateMovie")
    public RespEntity<String> updateMovie(@RequestBody MovieSaveDto movieActorChange) {
        if (movieActorChange != null) {
            Integer mid = movieActorChange.getMid();
            if (mid == null) {
                throw new IllegalArgumentException("更新电影，则mid不可为空！");
            }
            Movie movieChange = new Movie(movieActorChange);
            movieChange.setMid(mid);
            movieService.updateByPrimaryKeySelective(movieChange);
            List<MovieActorCreateDto> actorList = movieActorChange.getActorList();
            for (MovieActorCreateDto createDto : actorList) {
                // 删了全部旧的，新增新的
                Integer relationDelId = movieActorRelationMapper.deleteByAidAndMid(createDto.getAid(), mid);
                Integer relationIdNew = movieActorRelationService.insert(new MovieActorRelation(mid, createDto));
                if (relationDelId == null || relationIdNew == null) {
                    throw new IllegalArgumentException("更新电影——演员关系失败！");
                }
            }
            return RespEntity.successNoData("更新电影成功！");
        }
        return RespEntity.errorNoData(Constants.FLAG_400, "更新实体为null！");
    }
}
